package com.hzdl.cinema.web.servlet;

import com.hzdl.cinema.domain.PageBean;
import com.hzdl.cinema.domain.Play;
import com.hzdl.cinema.service.RepertoireService;
import com.hzdl.cinema.service.impl.RepertoireServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/repertoire/*")
public class RepertoireServlet extends BaseServlet {
       RepertoireService repertoireService =new RepertoireServiceImpl();
   public void playRepertoire(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String rows = request.getParameter("rows");
//       System.out.println(rows);
       String currentPage = request.getParameter("currentPage");
       String select_button = request.getParameter("select_button");
//       System.out.println(select_button);
       PageBean<Play> pageBean=repertoireService.playRepertoire(Integer.valueOf(rows),Integer.valueOf(currentPage),select_button);
        writeValue(response,pageBean);
   }

    public void addPlay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Play play=new Play();
        try {
            BeanUtils.populate(play, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        repertoireService.addPlay(play);

        writeValue(response,play);
    }

    public void deletePlay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String id=request.getParameter("id");
       repertoireService.deletePlay(id);


    }

    public void playEcho(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
          Play play =  repertoireService.playEcho(id);
          writeValue(response,play);
    }

    public void playUpdete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
//        String playName = request.getParameter("playName");
//        String playLength = request.getParameter("playLength");
//        String playType = request.getParameter("playType");
//        String playImage = request.getParameter("playImage");
//        String playDescription = request.getParameter("playDescription");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Play play=new Play();
        play.setPlayId(Integer.valueOf(id));
        try {
            BeanUtils.populate(play, parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        repertoireService.playUpdete(play);
        writeValue(response,play);
    }
    }
